package com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.back.bizactions;
import com.inspur.edp.bef.api.exceptions.BizMessageException;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.api.message.BizMessage;
import com.inspur.edp.cef.api.message.IBizMessage;
import com.inspur.edp.cef.api.message.MessageLevel;
import com.inspur.edp.cef.api.message.MessageLocation;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.back.comm.MxEntiy;

import java.util.ArrayList;
import java.util.HashMap;

public class sbssYhpHzdkjKcMgrAction extends AbstractManagerAction<VoidActionResult> {
	// 互转单ID
	private String id;
	public sbssYhpHzdkjKcMgrAction(IBEManagerContext managerContext, String id) {
		super(managerContext);
		this.id = id;
	}

	private String hzdCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.back.sbssYhpHzd";
	private String kcCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.sbssYhpItem";

	/**
	 * 锁定库存
	 * 1.制作锁定日志
	 * 2.锁定剩余数量
	 */
	@Override
	public  void execute() {
		IStandardLcp lcp = LcpUtil.getLcp(hzdCfg);
		IStandardLcp kcLcp = LcpUtil.getLcp(kcCfg);
		// hzd
		IEntityData hzdData = lcp.retrieve(this.id).getData();
		IEntityDataCollection hzdMx = hzdData.getChilds().get("hzdMx");

		// 合并锁定数量,确保单号合一
		HashMap<String, MxEntiy> mxMap = new HashMap<>();
		for (IEntityData mx : hzdMx) {
			String mxId = mx.getID();
			String zcsl = mx.getValue("zcsl").toString();
			IValuesContainer kcdh = (IValuesContainer) mx.getValue("kcdh");
			if (kcdh == null || kcdh.getValue("kcdh") == null) {
				break;
			}

			String kcId = kcdh.getValue("kcdh").toString();
			if (kcId == null){
				break;
			}
			String wlMc = kcdh.getValue("szdh_wlMc").toString();
			String wlXh = kcdh.getValue("szdh_wlXh").toString();
			MxEntiy mxEntiy = new MxEntiy(mxId, zcsl, kcId, wlMc, wlXh);

			if (!mxMap.containsKey(kcId)) {
				mxMap.put(kcId, mxEntiy);
			}else {
				int zcslInt = Integer.parseInt(zcsl);
				mxMap.get(kcId).addZcsl(zcslInt);
			}
		}

		mxMap.forEach((k,v)->{
			lockKc(hzdData,v,kcLcp);
		});
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}

	// 锁定库存
	private void lockKc (IEntityData hzdData,MxEntiy mx,IStandardLcp kcLcp){
		String mxId = mx.getId();
		int lockNum = mx.getZcsl();
		String kcId = mx.getKcdh();
		IEntityData kcData = kcLcp.retrieve(kcId).getData();
		int sysl = Integer.parseInt(kcData.getValue("sysl").toString());
		if(sysl < lockNum){
			IBizMessage mes = new BizMessage();
			mes.setLevel(MessageLevel.Error);
			MessageLocation location = new MessageLocation();
			location.getDataIds().add(mxId);
			location.setNodeCode(hzdData.getValue("djbh").toString());
			mes.setLocation(location);
			mes.setMessageFormat(mx.getWlMc() + ":转出数量大于剩余数量,请刷新页面并重新检查转出数量!");
			throw new BizMessageException("库存不足!", mes);
		}

		// 检查扣减日志是否存在,存在则返回
		IEntityDataCollection itemtras = kcData.getChilds().get("itemtras");
		boolean hasItem = itemtras.stream().anyMatch(item -> item.getValue("djMxid") != null && item.getValue("djMxid").toString().equals(mxId));
		if (hasItem) return;

		// 扣减剩余数量
		ModifyChangeDetail kcMcd = new ModifyChangeDetail(kcId);
		kcMcd.putItem("sysl", sysl - lockNum);

		ArrayList<String> nodeCodes = new ArrayList<>();
		nodeCodes.add("itemtras");
		ArrayList<String> hierarchyIds = new ArrayList<>();
		hierarchyIds.add(kcId);
		IEntityData kcChild = kcLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

		// 生成扣减日志
		ModifyChangeDetail mxDetail = new ModifyChangeDetail(kcChild.getID());
		mxDetail.getPropertyChanges().put("trasType","1");//互转
		mxDetail.getPropertyChanges().put("trasStatus","0");//待审核
		mxDetail.getPropertyChanges().put("omission","1");//出账
		mxDetail.getPropertyChanges().put("trasSl",lockNum);//转出数量
		mxDetail.getPropertyChanges().put("djid",hzdData.getID());//流转单据
		mxDetail.getPropertyChanges().put("djMxid",mxId);//流转明细ID
		mxDetail.getPropertyChanges().put("djCode",hzdData.getValue("djbh"));//单据号

		kcMcd.addChildChangeSet("itemtras",mxDetail);

		// 提交
		kcLcp.modify(kcMcd);
		kcLcp.save();
	}



}